package cn.charlotte.pit.util.item;

import de.tr7zw.nbtapi.NBT;
import de.tr7zw.nbtapi.iface.ReadWriteNBT;
import de.tr7zw.nbtapi.iface.ReadableNBT;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;

/**
 * @Author: EmptyIrony
 * @Date: 2021/1/1 1:37
 */
public class ItemUtil {


    public static String getUUID(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return null;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return null;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            if (extra.hasTag("uuid")) {
                return extra.getString("uuid");
            }
            return null;
        });
    }


    public static boolean isIllegalItem(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.modify(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return true;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            return !extra.hasTag("internal") || getInternalName(item).endsWith("_reward");
        });
    }

    public static boolean canDrop(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return false;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            return extra.hasTag("tradeAllow") && extra.getBoolean("tradeAllow");
        });
    }

    public static boolean isHealingItem(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return false;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            return extra.hasTag("isHealingItem") && extra.getBoolean("isHealingItem");
        });
    }

    public static boolean canTrade(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return false;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            return (extra.hasTag("canTrade") && extra.getBoolean("canTrade")) || (getInternalName(item) != null && getInternalName(item).startsWith("mythic_"));
        });
    }

    public static boolean canSaveEnderChest(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return false;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            return extra.hasTag("enderChest") && extra.getBoolean("enderChest");
        });
    }

    public static boolean isDefaultItem(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return false;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            return extra.hasTag("defaultItem") && extra.getBoolean("defaultItem");
        });
    }

    public static boolean isDeathDrop(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.modify(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return false;
            }
            ReadWriteNBT extra = nbt.getCompound("extra");

            if (extra.getString("internal").equals("beast_mode_helmet")) {
                return false;
            }

            return extra.hasTag("deathDrop") && extra.getBoolean("deathDrop");
        });
    }

    public static boolean isRemovedOnJoin(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return false;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return false;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            return extra.hasTag("removeOnJoin") && extra.getBoolean("removeOnJoin");
        });
    }

    public static String getInternalName(ItemStack item) {
        if (item == null || item.getType() == Material.AIR) {
            return null;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return null;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            if (extra.hasTag("internal")) {
                return extra.getString("internal");
            }
            return null;
        });
    }

    public static Integer getItemIntData(ItemStack item, String key) {
        if (item == null || item.getType() == Material.AIR) {
            return null;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return null;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            if (extra.hasTag(key)) {
                return extra.getInteger(key);
            }
            return null;
        });
    }

    public static String getItemStringData(ItemStack item, String key) {
        if (item == null || item.getType() == Material.AIR) {
            return null;
        }

        return NBT.get(item, nbt -> {
            if (!nbt.hasTag("extra")) {
                return null;
            }
            ReadableNBT extra = nbt.getCompound("extra");
            if (extra.hasTag(key)) {
                return extra.getString(key);
            }
            return null;
        });
    }

}
